﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace AliveMetro.Utils
{
    public class FindChildUtil
    {
        public static T FindChildOfType<T> (DependencyObject root) where T : class
        {
            var queue = new Queue<DependencyObject> ();
            queue.Enqueue (root);

            while (queue.Count > 0)
            {
                DependencyObject current = queue.Dequeue ();
                for (int i = VisualTreeHelper.GetChildrenCount (current) - 1; 0 <= i; i--)
                {
                    var child = VisualTreeHelper.GetChild (current, i);
                    var typedChild = child as T;
                    if (typedChild != null)
                    {
                        return typedChild;
                    }
                    queue.Enqueue (child);
                }
            }
            return null;
        }
    }
}
